
<?php
session_start ();
include '../base/validateAdminLogin.php';
include '../class/MMysql.php';

if (isset($_COOKIE["user_id"]))
    $user_id= $_COOKIE["user_id"];

$mysql = new MMysql();
$user = $mysql
    ->where(array('user_id'=>$user_id,"del_flag"=>0))
    ->select('user');

$error=$error2="";


if (isset ( $_POST ['oldpassword'] )) {

    $oldpassword = $_POST ['oldpassword'];
    $newpassword = $_POST ['newpassword'];
    $newpasswodagin = $_POST ['newpasswodagin'];

    if ($newpassword !=$newpasswodagin) {
        $error = "<span class='error'>Two passwords are not consistent</span>";
    }
    else if($newpassword=="" || $newpasswodagin==""||$oldpassword==""){
        $error2 = "<span class='error'>please input</span>";
    }
    else {
        $mysql = new MMysql();
        $count = $mysql
            ->where(array('password'=>$oldpassword,'user_id'=>$user_id))
        ->count("user");

        if($count>0){
            $res = $mysql
                ->where(array('user_id'=>$user_id))
                ->update('user',array('password'=>$newpassword));
            if ($res>0) {
                echo "<script type=text/javascript>alert('edit success');</script>";
            } else {

                // $error = "<span class='error'>edit is wrong！</span>";
            }
        }
        else{
            echo "<script type=text/javascript>alert('old password is  wrong');</script>";
        }


    }
}
?>

<?php
include '../base/layout.php';
?>

<body>

<?php
include '../base/header.php';
?>

<section style="width: 100%;">

    <?php
    include '../base/sidemenu.php';
    ?>

    <aside class="content">

        <div style="margin-top: 60px;margin-left: 100px;">
                <span style="position: relative;line-height: 34px;font-size:20px;color: rgb(152,152,152);display: block;">
                    change my password
                </span>
<form action="settings_page_password.php" method="post">
            <input type="text" class="form-control" style="margin-top: 20px;width: 400px;"
                   placeholder="old password" name="oldpassword">
            <?php
            echo $error2;
            ?>

            <input type="text" class="form-control" style="margin-top: 20px;width: 400px;"
                   placeholder="new password" name="newpassword">



            <input type="text" class="form-control" style="margin-top: 20px;width: 400px;"
                   placeholder="old password again" name="newpasswodagin">

    <?php
    echo $error;
    ?>
            <div style="margin-top: 20px;">
                <input class="btn" value="Confirm" type="submit" style="background-color: #2B2B2B;color:white;width: 100px;" />

            </div>
</form>
        </div>

    </aside>

</section>

</body>
</html>

